import { sessionStorage } from '@/utils/index'
export default {
  namespaced: true,
  state: {
    list: sessionStorage('playlist') || []
  },
  mutations: {
    // 添加到播放列表(单独添加一首歌)
    ADD_PLAYLIST(state, song) {
      if(state.list.some(item => item.songNo === song.songNo)) return
      state.list.unshift(song)
      sessionStorage('playlist', state.list)
    },
    // 替换播放列表
    REPLACE_PLAYLIST(state, songlist) {
      state.list = songlist
      sessionStorage('playlist', state.list)
    },
    // 清空播放列表
    CLEAR_PLAYLIST(state) {
      state.list = []
      sessionStorage('playlist', [])
    },
    // 移除列表
    REMOVE_PLAYLIST(state, songNo) {
      if (Array.isArray(songNo)) {
        // 批量移除
        state.list = state.list.filter(item => !songNo.includes(item.songNo))
      } else {
        // 单个移除
        state.list = state.list.filter(item => item.songNo !== songNo)
      }
      sessionStorage('playlist', state.list)
    }
  },
  actions: {}
}
